package com.example.video.controller;

import com.example.video.common.ResultModel;
import com.example.video.common.ResultStatus;
import com.example.video.domain.Device;
import com.example.video.service.inter.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Slf4j
@RestController
@RequestMapping("/device")
@Api(value = "设备相关接口", tags = {"DeviceController"})
public class DeviceController {

    private final IDeviceService deviceService;

    @Autowired
    public DeviceController(IDeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @RequiresUser
    @RequestMapping(method = POST)
    @ApiOperation(value = "创建/更新设备信息")
    public Object addOrUpdateDevice(@RequestBody Device device) {
        deviceService.addOrUpdateDevice(device);
        return ResultModel.ok();
    }

    @RequiresUser
    @RequestMapping(method = DELETE)
    @ApiOperation(value = "删除设备")
    public Object deleteDevices(@RequestBody List<Long> deviceIds) {
        deviceService.deleteDevices(deviceIds);
        return ResultModel.ok();
    }

    @RequiresUser
    @RequestMapping(method = GET)
    @ApiOperation(value = "获取设备列表", response = Device.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "page", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "size", paramType = "query")
    })
    public Object getDeviceList(@RequestParam(value = "page", defaultValue = "0") int page,
                                @RequestParam(value = "size", defaultValue = "10") int size) {
        List<Device> devices = deviceService.getDeviceList(page, size);
        if (CollectionUtils.isEmpty(devices))
            return ResultModel.error(ResultStatus.NO_DATA);
        return ResultModel.ok(devices);
    }

}
